@include('admin._include.header')
<style>
    .layui-form-label{
        width: 160px;
        text-align: left;
    }
    .layui-form-item .layui-input-inline{
        width: 290px;
    }
    .layui-btn{
        width: 200px;
    }
</style>
<body class="auth-user" style="background: #fff">
<div class="layui-fluid">
    <div class="layui-row larryms-panel auth-user-add">
        <form class="layui-form" method="post">

            @foreach($list as $k => $v)
            <div class="layui-form-item">
                <label class="layui-form-label">{{$v->name}}</label>
                <div class="layui-input-inline">
                    @if($v->type == 1)
                        <input type="text" name="array[{{$v->key}}]"   placeholder="请填写{{$v->name}}"  value="{{$v->value}}" autocomplete="off" class="layui-input larry-input">
                    @elseif($v->type == 2 )
                        <select name="array[{{$v->key}}]" lay-verify="required">
                            <option value="0"  @if($v->value == 0) selected @endif>关闭</option>
                            <option value="1"  @if($v->value == 1) selected @endif>开启</option>
                        </select>
                    @endif
                </div>
            </div>
            @endforeach


            <div class="layui-form-item" style="text-align: center">
                <button class="layui-btn" lay-submit lay-filter="ad_add">确定</button>
            </div>

        </form>
    </div>
</div>

<script>
    layui.config({
        base: "/plugin/layuiadmin/" //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'upload','form'], function(){
        var $ = layui.jquery,
            upload = layui.upload;
        form = layui.form;
        var index_upload;
        var uploadInst = upload.render({
            elem: '#test-upload-normal',
            url: '{{url('api/public/upload')}}',
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            multiple: false,  //是否允许多图上传
            accept: 'images',  //指定允许上传时校验的文件类型，可选值有：images（图片）、file（所有文件）、video（视频）、audio（音频）
            acceptMime: 'image/*',
            field: 'file',  //上传文件参数名
            before: function(obj){
                //预读本地文件示例，不支持ie8
                index_upload = layer.load(1);
            },
            done: function(res){
                layer.close(index_upload);
                //如果上传失败
                if(res.status != SUCCESS){
                    return layer.msg(res.message);
                }else{
                    $("input[name='images']").val(res.data);
                    $('#test-upload-normal-img').attr('src', res.data);
                }
                //上传成功
            },
            error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#test-upload-demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });


        form.on('submit(ad_add)', function(data) {
            var index = layer.load(1);
            $.ajax({
                url:"{{url('config/index')}}",
                dateType:'json',
                data:data.field,
                type:'post',
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                success:function (res) {
                    layer.close(index);
                    layer.msg(res.message);
                    if(res.status == SUCCESS){
                        var iframe = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(iframe); //再执行关闭
                    }
                }
            });

            return false;
        });


    });
</script>

</body>
@include('admin._include.footer')